home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / ip / ka9q / net_src.arc / sys5unix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-08  |  7.0 KB  |  434 lines

  1. /*
  2.     FILE: unix.c
  3.     
  4.     Routines: This file contains the following routines:
  5.         fileinit()
  6.         eihalt()
  7.         kbread()
  8.         clksec()
  9.         tmpfile()
  10.         restore()
  11.         stxrdy()
  12.         disable()
  13.         memstat()
  14.         filedir()
  15.         sysreset()
  16.         
  17.     Written by Mikel Matthews, N9DVG
  18.     SYS5 stuff added by Jere Sandidge, K4FUM
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <signal.h>
  23. #include <termio.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <sys/times.h>
  27. #include <time.h>
  28.  
  29. #include "global.h"
  30. #include "cmdparse.h"
  31. #include "iface.h"
  32. #include "unix.h"
  33. #include "ndir.h"
  34.  
  35. #define    MAXCMD    1024
  36.  
  37. int asy_attach();
  38.  
  39. extern struct cmds attab[];
  40. extern struct termio savecon;
  41.  
  42. unsigned nasy;
  43.  
  44. fileinit()
  45. {
  46.     int el;
  47.     char *ep, *cp, *malloc(), *getenv();
  48.     extern char *startup, *config, *userfile, *hosts, *mailspool;
  49.     extern char *mailqdir, *mailqueue, *routeqdir, *alias;
  50. #ifdef    FINGER
  51.     extern char *fingerpath;
  52. #endif
  53.  
  54.     /* Try to get home directory name */
  55.     if ((ep = getenv("NETHOME")) == NULLCHAR) {
  56.         if ((ep = getenv("HOME")) == NULLCHAR) {
  57.             ep = ".";
  58.         }
  59.     }
  60.     el = strlen(ep);
  61.     /* Replace each of the file name strings with the complete path */
  62.     if ((cp = malloc(el + strlen(startup) + 2)) == NULL)
  63.         perror("malloc");
  64.     else {
  65.         sprintf(cp, "%s/%s", ep, startup);
  66.         startup = cp;
  67.     }
  68.     
  69.     if ((cp = malloc(el + strlen(config) + 2)) == NULL)
  70.         perror("malloc");
  71.     else {
  72.         sprintf(cp, "%s/%s", ep, config);
  73.         config = cp;
  74.     }
  75.     
  76.     if ((cp = malloc(el + strlen(userfile) + 2)) == NULL)
  77.         perror("malloc");
  78.     else {
  79.         sprintf(cp, "%s/%s", ep, userfile);
  80.         userfile = cp;
  81.     }
  82.     
  83.     if ((cp = malloc(el + strlen(hosts) + 2)) == NULL)
  84.         perror("malloc");
  85.     else {
  86.         sprintf(cp, "%s/%s", ep, hosts);
  87.         hosts = cp;
  88.     }
  89.     
  90.     if ((cp = malloc(el + strlen(alias) + 2)) == NULL)
  91.         perror("malloc");
  92.     else {
  93.         sprintf(cp, "%s/%s", ep, alias);
  94.         alias = cp;
  95.     }
  96.     
  97. #ifdef    FINGER
  98.     if ((cp = malloc(el + strlen(fingerpath) + 2)) == NULL)
  99.         perror("malloc");
  100.     else {
  101.         sprintf(cp, "%s/%s", ep, fingerpath);
  102.         fingerpath = cp;
  103.     }
  104. #endif
  105.     
  106.     /* Try to get home directory name */
  107.     if ((ep = getenv("NETSPOOL")) == NULLCHAR)
  108.         ep = "/usr/spool";
  109.     el = strlen(ep);
  110.  
  111.     if ((cp = malloc(el + strlen(mailspool) + 2)) == NULL)
  112.         perror("malloc");
  113.     else {
  114.         sprintf(cp, "%s/%s", ep, mailspool);
  115.         mailspool = cp;
  116.     }
  117.     
  118.     if ((cp = malloc(el + strlen(mailqdir) + 2)) == NULL)
  119.         perror("malloc");
  120.     else {
  121.         sprintf(cp, "%s/%s", ep, mailqdir);
  122.         mailqdir = cp;
  123.     }
  124.     
  125.     if ((cp = malloc(el + strlen(mailqueue) + 2)) == NULL)
  126.         perror("malloc");
  127.     else {
  128.         sprintf(cp, "%s/%s", ep, mailqueue);
  129.         mailqueue = cp;
  130.     }
  131.     
  132.     if ((cp = malloc(el + strlen(routeqdir) + 2)) == NULL)
  133.         perror("malloc");
  134.     else {
  135.         sprintf(cp, "%s/%s", ep, routeqdir);
  136.         routeqdir = cp;
  137.     }
  138.     
  139. }
  140.  
  141. /* action routine for remote reset */
  142. sysreset()
  143. {
  144.     extern char *netexe;
  145.  
  146.     execl(netexe,netexe,0);
  147.     execl("net","net",0);
  148.     printf("reset failed: exiting\n");
  149.     exit(1);
  150. }
  151.  
  152. eihalt()
  153. {
  154.     tnix_scan();
  155. }
  156.  
  157.  
  158. kbread()
  159. {
  160.     unsigned char c;
  161.  
  162.     if (read(fileno(stdin), &c, 1) <= 0)
  163.         return -1;
  164.  
  165.     return ((int) c);
  166. }
  167.  
  168.  
  169. clksec()
  170. {
  171.     long tim;
  172.  
  173.     (void) time(&tim);
  174.  
  175.     return ((int) tim);
  176. }
  177.  
  178.  
  179. restore()
  180. {
  181. }
  182.  
  183.  
  184. stxrdy()
  185. {
  186.     return 1;
  187. }
  188.  
  189.  
  190. disable()
  191. {
  192. }
  193.  
  194.  
  195. memstat()
  196. {
  197.     return 0;
  198. }
  199.  
  200.  
  201. /* wildcard filename lookup */
  202. filedir(name, times, ret_str)
  203. char    *name;
  204. int    times;
  205. char    *ret_str;
  206. {
  207.     static char    dname[128], fname[128];
  208.     static DIR *dirp = NULL;
  209.     struct direct *dp;
  210.     struct stat sbuf;
  211.     char    *cp, temp[128];
  212.  
  213.     /*
  214.      * Make sure that the NULL is there in case we don't find anything
  215.      */
  216.     ret_str[0] = '\0';
  217.  
  218.     if (times == 0) {
  219.         /* default a null name to *.* */
  220.         if (name == NULL || *name == '\0')
  221.             name = "*.*";
  222.         /* split path into directory and filename */
  223.         if ((cp = strrchr(name, '/')) == NULL) {
  224.             strcpy(dname, ".");
  225.             strcpy(fname, name);
  226.         } else {
  227.             strcpy(dname, name);
  228.             dname[cp - name] = '\0';
  229.             strcpy(fname, cp + 1);
  230.             /* root directory */
  231.             if (dname[0] == '\0')
  232.                 strcpy(dname, "/");
  233.             /* trailing '/' */
  234.             if (fname[0] == '\0')
  235.                 strcpy(fname, "*.*");
  236.         }
  237.         /* close directory left over from another call */
  238.         if (dirp != NULL)
  239.             closedir(dirp);
  240.         /* open directory */
  241.         if ((dirp = opendir(dname)) == NULL) {
  242.             printf("Could not open DIR (%s)\n", dname);
  243.             return;
  244.         }
  245.     } else {
  246.         /* for people who don't check return values */
  247.         if (dirp == NULL)
  248.             return;
  249.     }
  250.  
  251.     /* scan directory */
  252.     while ((dp = readdir(dirp)) != NULL) {
  253.         /* test for name match */
  254.         if (wildmat(dp->d_name, fname)) {
  255.             /* test for regular file */
  256.             sprintf(temp, "%s/%s", dname, dp->d_name);
  257.             if (stat(temp, &sbuf) < 0)
  258.                 continue;
  259.             if ((sbuf.st_mode & S_IFMT) != S_IFREG)
  260.                 continue;
  261.             strcpy(ret_str, dp->d_name);
  262.             break;
  263.         }
  264.     }
  265.  
  266.     /* close directory if we hit the end */
  267.     if (dp == NULL) {
  268.         closedir(dirp);
  269.         dirp = NULL;
  270.     }
  271. }
  272.  
  273.  
  274. /* checks the time then ticks and updates ISS */
  275. void
  276. check_time()
  277. {
  278.     int32 iss();
  279.     long times();
  280.  
  281.     struct tms tb;
  282.     static long clkval;
  283.     long ntime, offset;
  284.  
  285.     /* read elapsed real time (typ. 60 Hz) */
  286.     ntime = times(&tb);
  287.  
  288.     /* resynchronize if the error is large (10 seconds or more) */
  289.     offset = ntime - clkval;
  290.     if (offset > (10000/MSPTICK) || offset < 0)
  291.         clkval = ntime;
  292.  
  293.     /* Handle possibility of several missed ticks */
  294.     while (ntime != clkval) {
  295.         ++clkval;
  296.         icmpclk();
  297.         tick();
  298.         (void) iss();
  299.     }
  300. }
  301.  
  302.  
  303. getds()
  304. {
  305.     return 0;
  306. }
  307.  
  308.  
  309. audit()
  310. {
  311. }
  312.  
  313.  
  314. doshell(argc, argv)
  315. char    **argv;
  316. {
  317.     int    i, stat;
  318.     char    str[MAXCMD];
  319.     char    *cp;
  320.     struct termio tt_config;
  321.  
  322.     char    *getenv();
  323.  
  324.     str[0] = '\0';
  325.     for (i = 1; i < argc; i++) {
  326.         strcat(str, argv[i]);
  327.         strcat(str, " ");
  328.     }
  329.  
  330.     ioctl(0, TCGETA, &tt_config);
  331.     ioctl(0, TCSETAW, &savecon);
  332.  
  333.     if (argc > 1)
  334.         stat = system(str);
  335.     else if ((cp = getenv("SHELL")) != NULL && *cp != '\0')
  336.         stat = system(cp);
  337.     else
  338.         stat = system("exec /bin/sh");
  339.  
  340.     ioctl(0, TCSETAW, &tt_config);
  341.  
  342.     return stat;
  343. }
  344.  
  345.  
  346. dodir(argc, argv)
  347. int    argc;
  348. char    **argv;
  349. {
  350.     int    i, stat;
  351.     char    str[MAXCMD];
  352.     struct termio tt_config;
  353.  
  354.     strcpy(str, "ls -l ");
  355.     for (i = 1; i < argc; i++) {
  356.         strcat(str, argv[i]);
  357.         strcat(str, " ");
  358.     }
  359.  
  360.     ioctl(0, TCGETA, &tt_config);
  361.     ioctl(0, TCSETAW, &savecon);
  362.  
  363.     stat = system(str);
  364.  
  365.     ioctl(0, TCSETAW, &tt_config);
  366.  
  367.     return stat;
  368. }
  369.  
  370.  
  371. rename(s1, s2)
  372. char *s1, *s2;
  373. {
  374.     char tmp[MAXCMD];
  375.  
  376.     (void) sprintf(tmp, "mv %s %s", s1, s2);
  377.     (void) system(tmp);
  378. }
  379.  
  380.  
  381. int
  382. docd(argc, argv)
  383. int argc;
  384. char **argv;
  385. {
  386.     char tmp[MAXCMD];
  387.     char *getcwd();
  388.  
  389.     if (argc > 1) {
  390.         if (chdir(argv[1]) == -1) {
  391.             printf("Can't change directory\n");
  392.             return 1;
  393.         }
  394.     }
  395.     if (getcwd(tmp, sizeof(tmp)) == NULL)
  396.         printf("%s\n", tmp);
  397.  
  398.     return 0;
  399. }
  400.  
  401.  
  402. ether_dump()
  403. {
  404. }
  405.  
  406.  
  407. mkdir(s, m)
  408. char    *s;
  409. int    m;
  410. {
  411.     char tmp[MAXCMD];
  412.  
  413.     sprintf(tmp, "mkdir %s", s);
  414.     if (system(tmp))
  415.         return -1;
  416.     if (chmod(s, m) < 0)
  417.         return -1;
  418.  
  419.     return 0;
  420. }
  421.  
  422.  
  423. rmdir(s)
  424. char    *s;
  425. {
  426.     char tmp[MAXCMD];
  427.  
  428.     sprintf(tmp, "rmdir %s", s);
  429.     if (system(tmp))
  430.         return -1;
  431.  
  432.     return 0;
  433. }
  434.